package top.R3.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.FlowPane;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.R3.disk.Disk;
import top.R3.domain.MyTreeItem;
import top.R3.filesys.FileSysHandler;
import top.R3.process.PCB;

import javax.annotation.Resource;

/**
 * @Author lemon
 * @Date 2022/9/13 20:09
 * @Version 1.0
 */
@Slf4j
@Data
@FXMLController
public class FileSysController {
    //存放小矩形的盒子
    @FXML
    public FlowPane rectBox;
    //表格
    @FXML
    public ObservableList<Disk> diskList = FXCollections.observableArrayList();
    public TableView<Disk> diskTable;
    public TableColumn<Disk, IntegerProperty> num;
    public TableColumn<Disk, IntegerProperty> next;


    @Resource
    private FileSysHandler fileSysHandler;
    public void initialize(){
        //给矩形块命名id,并绑定表格和盘块
        ObservableList<Node> rectList = rectBox.getChildren();
        for(int i=0;i<rectList.size();i++){
            rectList.get(i).setId("rect"+String.valueOf(i));
            diskList.add(new Disk(i,i));
        }
        //绑定参数
        num.setCellValueFactory(new PropertyValueFactory<>("num"));
        next.setCellValueFactory(new PropertyValueFactory<>("next"));
        //表格绑定磁盘块
        diskTable.setItems(diskList);
        diskList.get(0).setNext(254);
        diskList.get(1).setNext(254);
        diskList.get(2).setNext(254);
        rectList.get(1).setStyle("-fx-fill: #11ff3b;");
        rectList.get(2).setStyle("-fx-fill: #11ff3b;");
    }

    //找空的磁盘块
    public int findFreeDisk(){
        int diskLen=diskList.size();
        for(int i=3;i<diskLen;i++){
            if(diskList.get(i).getNext()==0)
                return i;
        }
        return -1;
    }
}
